package com.cheng.cms.controller;


import com.cheng.cms.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.PanelUI;
import java.util.Arrays;
import java.util.stream.Collectors;


@Controller
@Api("登录路由")
public class LoginController {
    /**
     * 注入redis工具类
     */
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 首页展示
     * @return
     */
    @GetMapping("/sys/about")
    @ApiOperation("默认跳转到首页")
    public String about(){
        return "about";
    }

    /**
     * 访问8001端口默认跳转到登录页面
     * @return
     */
    @GetMapping("/")
    @ApiOperation("登录页面跳转")
    public String login(){
        return "login";
    }

    /**
     * 登录验证,从表单获取的数据经过shiro认证，通过则保存在令牌中
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("点击登录分析表单数据")
    public String login(String username, String password){
        //System.out.println(username+password);
        if(username==null||username.length()==0){
            //用户名为空重定向回到登录页面
            return "fail";
        }
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//        long timeout = SecurityUtils.getSubject().getSession().getTimeout();
//        System.out.println(timeout+"毫秒");
        // 执行认证登陆
        try {
            subject.login(token);
        }catch (Exception e){
            return "fail";
        }
        if (subject.isAuthenticated()) {
            return "success";
        } else {
            token.clear();
            return "fail";
        }

    }

    /**
     * 验证成功后跳转首页
     * @param modelMap
     * @return
     */
    @ApiOperation("跳转到首页")
    @GetMapping("/index")
    public String index(ModelMap modelMap){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        modelMap.put("username",username);
        return "/index";
    }

    /**
     * 退出登录
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "redirect:/";
    }

    /**
     * 身份验证失败访问登录
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("访问login页面")
    public String toLogin(){
        return "/login";
    }

    /**
     * 权限认证失败，则跳转到指定页面
     * @return
     */
    @GetMapping("/unauth")
    public String unauth()
    {
        return "error/unauth";
    }

}
